<template>
  <a-form-model ref="userForm" layout="vertical" :model="formInfo" :rules="rules">
    <a-row :gutter="24">
      <a-col :span="12">
        <a-form-model-item label="选择单位">
          <a-cascader
              :options="options"
              placeholder="请选择"
              change-on-select
              @blur="onChange"
              v-model="formInfo.department"
          />
          <!--          <a-input v-else v-model="form.userName" placeholder="" disabled/>-->
        </a-form-model-item>
      </a-col>
      <a-col :span="12">
        <a-form-model-item label="姓名">
          <a-input v-model="formInfo.fullName" placeholder="" />
        </a-form-model-item>
      </a-col>
    </a-row>
    <a-row :gutter="24">
      <a-col :span="12">
        <a-form-model-item label="手机号码">
          <a-input v-model="formInfo.a0105" placeholder="" />
        </a-form-model-item>
      </a-col>
      <a-col :span="12">
        <zh-input type="select" label="人员性质" prop="h1104"
                  v-model="formInfo.h1102"
                  :options="dictData.H1102"
                  :defaultFields="{ label: 'dictValue', value: 'dictKey' }"
        ></zh-input>
      </a-col>
    </a-row>
    <a-row :gutter="24">
      <a-col :span="12">
        <zh-input type="number"  label="负责文保点" :disabled="false" v-model="formInfo.h1103" ></zh-input>
      </a-col>
    </a-row>
  </a-form-model>
</template>
<script>
import {
  getOrgTreeDataByPermission,
  getSQAndJD, saveCulturalOfficer,
  getOrg, getCultural, updateCulturalOfficer, getCulturalOfficer,
} from '@/services/system/whdngxlApi'
import {getBatchDict, getDictCondition} from "@/services/system/api"
import { TreeSelect } from 'ant-design-vue';
import UnmovableRelics from "@/pages/whdngxl/TasksCheckManage/UnmovableRelics";
import modal from "ant-design-vue/lib/modal";
const SHOW_PARENT = TreeSelect.SHOW_PARENT;
const SHOW_CHILD = TreeSelect.SHOW_CHILD;
export default {
  props: {
      listQuery:{
        type:Function,
        default:null
      }
    },
  data() {
    return {
      rules: {
        userName: [{ required: true, message: '请选择人员', trigger: 'blur' }],
      },
      dictData:{},
      options: [],
      formInfo: {
        //h11表数据
        h1101:'',
        b0122:'',
        userId:'',
        department:[],
        a0102:'',
        a0102Name:'',
        A0103:'',
        a0103Name:'',
        A0104:'',
        a0104Name:'',
        FULLName:'',
        a0105:'',
        h1102:'',
        h1103:'',
        h1104:'',
        userOrgIds:[],

      },
      SHOW_PARENT,
      SHOW_CHILD,
      orgTreeData: [],
      a0202DictList: [],
      a0202DictListTemp: [],
      editFlag: false,
      a0202SelectList: [],
      beforeOrgId: null,
      length: 0
    };
  },
  created() {
    const param = {}
    param.dictFields = 'H1102'
    this.$http.post(getBatchDict, param).then(res => {
      this.dictData = res.result
    })
    //获取区域
    this.$http.post(getOrgTreeDataByPermission, {}).then(res => {
      this.orgTreeData = res.result.orgList
    })
    this.$http.post(getOrg, {searchOrg: true}).then(res => {
      this.options = res.result
    })
    this.init();
  },
  methods:{
    init() {
     /* this.$http.post(getDictCondition, {dictField: 'h1104',classify: this.h1104}).then(res => {
        this.$set(this.dictData, 'h1104', res.result)
      })*/

      this.$http.post(getSQAndJD, {}).then((res) => {
        this.sqData = res.result.sqData;
        this.jdData = res.result.jdData;
        if (this.formData && this.formData.b0122) {
          this.jdDataArray = this.jdData[this.formData.b0122];
        }
      });
      /*//获取区域
       this.$http.post(getPermissionParams, {}).then(res => {
         const data = res.result
         this.taskAreaTree = data.orgTreeData
       })*/
    },
    onChange(value,options) {
      const data=this.formInfo.department
      this.formInfo.b0122=data[0];
      this.formInfo.a0102=data[1];
      this.formInfo.a0103=data[2];
    },
    onChangeTree(value, label, extra){
    },
    removePermission(item){
      let index = this.formInfo.permissionGroup.indexOf(item);
      if (index !== -1) {
        let a0202Index = -1
        this.a0202SelectList.forEach((e,eIndex) =>{
          if(e.index === index){
            a0202Index = eIndex
          }
        })
        if(a0202Index > -1){
          this.a0202SelectList.splice(a0202Index,1)
        }
        this.formInfo.permissionGroup.splice(index, 1);
      }
    },
    initData(data){
      // this.form.h1104 = data.h1101+''
      /*debugger*/
      this.formInfo.h1101 = data.h1101
      const params2 = {}
      params2.dictFields = 'H1102'
      this.$http.post(getBatchDict, params2).then(res => {
        this.dictData = res.result
        console.log(this.dictData)
        //获取筛选数据
        UnmovableRelics.methods.getFiltersData(this,'tasksList')
      })

      this.$nextTick(()=>{
        if(data){
          const params = {}
          params.h1101 = data.h1101
          console.log(data)
          /*params.userOrgIds = this.userOrgIds*/
          setTimeout(() => {
            this.$http.post(getCulturalOfficer, params).then((res) => {
              if (res.result) {
                this.formInfo.a0102Name=res.result.a0102Name
                this.formInfo.a0102=res.result.a0102
                this.formInfo.b0122=res.result.b0122
                this.formInfo.a0103Name=res.result.a0103Name
                this.formInfo.a0103=res.result.a0103
                this.formInfo.a0104Name=res.result.a0104Name
                this.formInfo.fullName=res.result.fullName
                this.formInfo.department=res.result.chooseJob
                this.formInfo.a0105=res.result.a0105
                this.formInfo.a0105=res.result.a0105
                this.formInfo.h1104=res.result.h1104
                this.formInfo.userOrgIds=res.result.userOrgIds
                this.formInfo.h1102=res.result.h1102
                this.formInfo.h1103=res.result.h1103
              }
            }).catch((error) => {
              console.log(JSON.stringify(error))
              this.$message.error('获取初始化数据失败')
            }).finally(() =>{
            })
          }, 50)
        }
      })

    },

    saveOrUpdate() {
      /*if(this.formInfo.c0302 == "" || this.formInfo.c0302 == null){
        modal.warn({title: '提示', content: '任务名称不能为空'})
        return
      }
      if(this.formInfo.c0303 == "" || this.formInfo.c0303 == null){
        modal.warn({title: '提示', content: '任务表不能为空'})
        return
      }
      if(this.formInfo.taskArea == undefined){
        modal.warn({title: '提示', content: '任务范围不能为空'})
        return
      }
      if(this.formInfo.c0304Time.length === 0){
        modal.warn({title: '提示', content: '任务时间不能为空'})
        return
      }*/
      // this.$loading.show('正在处理数据,请等待...')
      const params = Object.assign(this.formInfo)
      console.log(params.h1101)
      debugger
      if (this.formInfo.h1101==null ||this.formInfo.h1101==""){
        setTimeout(() => {
          this.$http.post(saveCulturalOfficer, params).then((res) => {
            this.$message.info('保存成功')
            this.$emit("handleCancel")
            if(this.listQuery){
              this.listQuery()
            }
            this.$loading.hide()
          }).catch((error) => {
            //console.log(JSON.stringify(error))
            this.$loading.hide()
          }).finally(() =>{
          })
        }, 50)
      }else {
        setTimeout(() => {
          this.$http.post(updateCulturalOfficer, params).then((res) => {
            this.$message.info('修改成功')
            this.$emit("handleCancel")
            if(this.listQuery){
              this.listQuery()
            }
            this.$loading.hide()
          }).catch((error) => {
            //console.log(JSON.stringify(error))
            this.$loading.hide()
          }).finally(() =>{
          })
        }, 50)
      }
    },
  },
  computed: {

  },
  watch: {
    'a0202SelectList': {
      handler() {
        const a0202Select  = []
        this.a0202SelectList.forEach(e =>{
          a0202Select.push(e.value)
        })
        this.a0202DictList.forEach((item,index) =>{
          if(a0202Select.indexOf(item.dictKey) > -1){
            this.a0202DictList.splice(index,1,{dictKey: this.a0202DictList[index].dictKey,
              dictValue: this.a0202DictList[index].dictValue,status: '0'})
          }else {
            this.a0202DictList.splice(index,1,{dictKey: this.a0202DictList[index].dictKey,
              dictValue: this.a0202DictList[index].dictValue,status: '1'})
          }
        })
      },
      deep: true
    }
  }
};
</script>
<style lang="less" scoped>
.ant-form-item {
  margin-bottom: 5px;
}
.custom {
  .ant-form-item-children {
    span {
      width: 95%;
    }
  }
}
</style>
